#include "myword.h"
#include "ui_myword.h"

Myword::Myword(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Myword)
{
    ui->setupUi(this);
    //创建MDI区域
    mdiArea=new QMdiArea;
    //设置滚动条策略
    mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);//按需显示滚动条
    //将mdiArea设为主窗口的中央区域
    setCentralWidget(mdiArea);
    //自动更新工具
    connect(mdiArea,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(updateMenus()));
    //当不同的文件之间切换时，激活当前的文件，更新菜单和工具的状态

    //窗口切换器:告诉系统用户想切换到那个窗口
    //windowMapper=new QSignalMapper(this);
    //connect(windowMapper,&QSignalMapper::mappedObject, this, &Myword::activateWindow);

    this->createActions();
    this->createMenus();
    this->createToolBars();
    this->createStatusBars();


}

Myword::~Myword()
{
    delete ui;
}



void Myword::createActions()
{

    //编辑菜单动作创建
    newAct=new QAction("新建(&N)",this);
    newAct->setShortcut(QKeySequence::New);//新建为new
    //忘记了状态栏和工具栏
    newAct->setToolTip("新建");
    newAct->setStatusTip("新建一个word文档");
    //连接新建的槽函数
    connect(newAct,SIGNAL(triggered()),this,SLOT(fileNew()));
    //打开
    openAct=new QAction("打开(&O)",this);
    openAct->setShortcut(QKeySequence::Open);
    openAct->setToolTip("打开");
    openAct->setStatusTip("打开一个word文件");
    connect(openAct,SIGNAL(triggered()),this,SLOT(fileOpen()));
    //保存
    saveAct=new QAction("保存(&S)",this);
    saveAct->setShortcut(QKeySequence::Save);
    saveAct->setToolTip("保存");
    saveAct->setStatusTip("保存一个word文件");
    connect(saveAct,SIGNAL(triggered()),this,SLOT(fileSave()));
    //另存为
    saveAsAct=new QAction("另存为(&A)",this);
    saveAsAct->setShortcut(QKeySequence::SaveAs);
    saveAsAct->setToolTip("另存为");
    saveAsAct->setStatusTip("另存当前word文件");
    connect(saveAsAct,SIGNAL(triggered()),this,SLOT(fileSaveAs()));
    //打印
    printAct=new QAction("打印(&P)",this);
    printAct->setShortcut(QKeySequence::Print);
    printAct->setToolTip("打印");
    printAct->setStatusTip("打印当前word文件");
    connect(printAct,SIGNAL(triggered()),this,SLOT(filePrint()));
    //打印预览
    printPrevAct=new QAction("打印预览",this);
    printPrevAct->setStatusTip("打印预览word文件");
    connect(printPrevAct,SIGNAL(triggered()),this,SLOT(filePrintPreview()));
    //退出word
    exitAct=new QAction("退出(&X)",this);
    exitAct->setStatusTip("退出当前word");
    connect(exitAct,SIGNAL(triggered()),qApp,SLOT(closeAllWindows()));
    //编辑菜单
    //撤销
    undoAct=new QAction("撤销(&U)",this);
    undoAct->setShortcut(QKeySequence::Undo);
    undoAct->setToolTip("撤销");
    undoAct->setStatusTip("撤销当前word操作");
    connect(undoAct,SIGNAL(triggered()),this,SLOT(undo()));
    //重做
    redoAct=new QAction("重做(&R)",this);
    redoAct->setShortcut(QKeySequence::Redo);
    redoAct->setToolTip("重做");
    redoAct->setStatusTip("恢复之前word操作");
    connect(redoAct,SIGNAL(triggered()),this,SLOT(redo()));
    //剪切
    cutAct=new QAction("剪切(&T)",this);
    cutAct->setShortcut(QKeySequence::Cut);
    cutAct->setToolTip("剪切");
    cutAct->setStatusTip("剪切当前word文本放入剪切板");
    connect(cutAct,SIGNAL(triggered()),this,SLOT(cut()));
    //复制
    copyAct=new QAction("复制(&C)",this);
    copyAct->setShortcut(QKeySequence::Copy);
    copyAct->setToolTip("复制");
    copyAct->setStatusTip("复制当前文本");
    connect(copyAct,SIGNAL(triggered()),this,SLOT(copy()));
    //粘贴
    pasteAct=new QAction("粘贴(&P)",this);
    pasteAct->setShortcut(QKeySequence::Paste);
    pasteAct->setToolTip("粘贴");
    pasteAct->setStatusTip("粘贴到此处");
    connect(pasteAct,SIGNAL(triggered()),this,SLOT(paste()));
    //格式菜单
    boldAct=new QAction("加粗(&B)",this);
    boldAct->setShortcut(Qt::CTRL + Qt::Key_B);
    boldAct->setToolTip("加粗");
    boldAct->setStatusTip("对文本进行加粗");
    boldAct->setCheckable(true);

    //显示到这个action的字体上
    QFont bold;
    bold.setBold(true);
    boldAct->setFont(bold);
    connect(boldAct,SIGNAL(triggered()),this,SLOT(textBold()));

    //倾斜
    italicAct=new QAction(tr("倾斜(&I)"),this);
    italicAct->setCheckable(true);
    italicAct->setShortcut(Qt::CTRL + Qt::Key_I);
    italicAct->setToolTip("倾斜");
    italicAct->setStatusTip(tr("将你所选择的文字进行倾斜处理."));

    QFont italic;
    italic.setItalic(true);
    italicAct->setFont(italic);
    connect(italicAct,SIGNAL(triggered()),this,SLOT(textItalic()));
    // 下划线
    underlineAct=new QAction(tr("下划线(&U)"),this);
    underlineAct->setCheckable(true);
    underlineAct->setShortcut(Qt::CTRL + Qt::Key_U);
    underlineAct->setToolTip("下划线");
    underlineAct->setStatusTip(tr("将你所选择的文字添加下划线."));

    QFont underline;
    underline.setUnderline(true);
    underlineAct->setFont(underline);
    connect(underlineAct,SIGNAL(triggered()),this,SLOT(textUnderline()));


    //创建文本对齐方式的互斥选项组
    QActionGroup*grp=new QActionGroup(this);
    connect(grp,SIGNAL(triggered(QAction*)),this,SLOT(textAlign(QAction*)));

    if(QApplication::isLeftToRight())
    {
        leftAlignAct=new QAction(tr("左对齐(&L)"),grp);
        centerAct=new QAction(tr("居中对齐(&E)"),grp);
        rightAlignAct=new QAction(tr("右对齐(&R)"),grp);
    }
    else
    {
        rightAlignAct=new QAction(tr("右对齐(&R)"),grp);
        centerAct=new QAction(tr("居中对齐(&E)"),grp);
        leftAlignAct=new QAction(tr("左对齐(&L)"),grp);
    }
    justifyAct=new QAction(tr("两端对齐(&J)"),grp);
    leftAlignAct->setShortcut(Qt::CTRL + Qt::Key_L);
    leftAlignAct->setCheckable(true);
    leftAlignAct->setToolTip("左对齐");
    leftAlignAct->setStatusTip(tr("将选择文字进行左对齐."));

    centerAct->setShortcut(Qt::CTRL + Qt::Key_E);
    centerAct->setCheckable(true);
    centerAct->setToolTip("居中对齐");
    centerAct->setStatusTip(tr("将选择文字进行居中对齐."));

    rightAlignAct->setShortcut(Qt::CTRL + Qt::Key_R);
    rightAlignAct->setCheckable(true);
    rightAlignAct->setToolTip("右对齐");
    rightAlignAct->setStatusTip(tr("将选择文字进行右对齐."));

    justifyAct->setShortcut(Qt::CTRL + Qt::Key_J);
    justifyAct->setCheckable(true);
    justifyAct->setToolTip("两端对齐");
    justifyAct->setStatusTip(tr("将选择文字进行两端对齐."));

    colorAct=new QAction("颜色(&C)",this);
    colorAct->setStatusTip("设置字体颜色");
    connect(colorAct,SIGNAL(triggered()),this,SLOT(textColor()));

    //窗口action
    closeAct=new QAction(tr("关闭(&O)"),this);
    closeAct->setStatusTip(tr("关闭活动word文档子窗口."));
    connect(closeAct,SIGNAL(triggered()),mdiArea,SLOT(closeActiveSubWindow()));

    closeAllAct=new QAction(tr("关闭所有(&A)"),this);
    closeAllAct->setStatusTip(tr("关闭活动word文档所有子窗口."));
    connect(closeAllAct,SIGNAL(triggered()),mdiArea,SLOT(closeAllSubWindows()));


    tileAct=new QAction(tr("平铺(&T)"),this);
    tileAct->setStatusTip(tr("平铺子窗口."));
    connect(tileAct,SIGNAL(triggered()),mdiArea,SLOT(tileSubWindows()));

    cascadeAct=new QAction(tr("层叠(&C)"),this);
    cascadeAct->setStatusTip(tr("层叠子窗口."));
    connect(cascadeAct,SIGNAL(triggered()),mdiArea,SLOT(cascadeSubWindows()));

    nextAct=new QAction(tr("下一个(&T)"),this);
    nextAct->setShortcuts(QKeySequence::NextChild);
    nextAct->setStatusTip(tr("移动焦点到下一个子窗口."));
    connect(nextAct,SIGNAL(triggered()),mdiArea,SLOT(activateNextSubWindow()));

    previousAct=new QAction(tr("前一个(&V)"),this);
    previousAct->setShortcuts(QKeySequence::PreviousChild);
    previousAct->setStatusTip(tr("移动焦点到前一个子窗口."));
    connect(previousAct,SIGNAL(triggered()),mdiArea,SLOT(activatePreviousSubWindow()));

    separatorAct=new QAction(this);
    separatorAct->setSeparator(true);//添加分割线，不执行任何操作

    //帮助菜单
    aboutAct=new QAction("关于（&A)",this);
    aboutAct->setStatusTip("关于Office Word相关信息.");
    connect(aboutAct,SIGNAL(triggered()),this,SLOT(about()));
    aboutQtAct=new QAction(tr("关于 Qt(&Qt)"),this);
    aboutQtAct->setStatusTip("关于 Qt库相关信息.");
    connect(aboutQtAct,SIGNAL(triggered()),this,SLOT(aboutQt()));
}

void Myword::createMenus()
{
    fileMenu=menuBar()->addMenu(tr("文件(&F)"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addSeparator();
    fileMenu->addAction(saveAct);
    fileMenu->addAction(saveAsAct);
    fileMenu->addSeparator();
    fileMenu->addAction(printAct);
    fileMenu->addAction(printPrevAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);
    //【编辑】主菜单
    editMenu = menuBar()->addMenu(tr("编辑(&E)"));
    editMenu->addAction(undoAct);
    editMenu->addAction(redoAct);
    editMenu->addSeparator();   //分隔线
    editMenu->addAction(cutAct);
    editMenu->addAction(copyAct);
    editMenu->addAction(pasteAct);
    //【格式】主菜单
    //字体
    formatMenu = menuBar()->addMenu(tr("格式(&O)"));
    fontMenu=formatMenu->addMenu("字体(&D)");
    fontMenu->addAction(boldAct);
    fontMenu->addAction(italicAct);
    fontMenu->addAction(underlineAct);
    //段落
    alignMenu=formatMenu->addMenu("段落");
    alignMenu->addAction(leftAlignAct);
    alignMenu->addAction(centerAct);
    alignMenu->addAction(rightAlignAct);
    alignMenu->addAction(justifyAct);

    formatMenu->addAction(colorAct);

    windowMenu=menuBar()->addMenu("窗口(&W)");
    updateWindowMenu();//在程序启动时更新窗口菜单的内容，显示当前已经打开的子窗口列表
    //连接菜单显示信号，当用户点击菜单时，菜单即将显示之前发出，来刷新窗口内容
    connect(windowMenu,SIGNAL(aboutToShow()),this,SLOT(updateWindowMenu()));
    // 在菜单中添加分隔线
    windowMenu->addAction(cascadeAct);    // 层叠
    windowMenu->addAction(nextAct);       // 下一个
    windowMenu->addAction(previousAct);   // 前一个
    windowMenu->addAction(separatorAct);  // 这里会显示分隔线 ←
    windowMenu->addAction(closeAct);      // 关闭窗口
    //在帮助之前添加分隔符
    menuBar()->addSeparator();

    helpMenu=menuBar()->addMenu("帮助(&H)");
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);



}
void Myword::createToolBars()
{
    filetoolbar=new QToolBar(this);
    this->addToolBar(filetoolbar);
    filetoolbar->addSeparator();
    filetoolbar->addAction(newAct);
    filetoolbar->addAction(openAct);
    filetoolbar->addAction(saveAct);
    filetoolbar->addSeparator();
    filetoolbar->addAction(printAct);
    filetoolbar->addSeparator();
    edittoolbar=new QToolBar(this);
    this->addToolBar(edittoolbar);
    edittoolbar->addAction(undoAct);
    edittoolbar->addAction(redoAct);
    edittoolbar->addSeparator();
    edittoolbar->addAction(cutAct);
    edittoolbar->addAction(copyAct);
    edittoolbar->addAction(pasteAct);
    edittoolbar->addSeparator();
    fomattoolbar=new QToolBar(this);
    this->addToolBar(fomattoolbar);
    fomattoolbar->addAction(boldAct);
    fomattoolbar->addAction(italicAct);
    fomattoolbar->addAction(underlineAct);
    fomattoolbar->addSeparator();
    fomattoolbar->addAction(leftAlignAct);
    fomattoolbar->addAction(centerAct);
    fomattoolbar->addAction(rightAlignAct);
    fomattoolbar->addAction(justifyAct);
    fomattoolbar->addAction(colorAct);

    addToolBarBreak(Qt::TopToolBarArea);    //使这个工具条在界面上另起一行显示
    comtoolbar=new QToolBar(this);
    this->addToolBar(comtoolbar);
    symbolcombobox=new QComboBox(this);
    symbolcombobox->addItem("标准");
    symbolcombobox->addItem("项目符号 (●)");
    symbolcombobox->addItem("项目符号 (○)");
    symbolcombobox->addItem("项目符号 (■)");
    symbolcombobox->addItem("编号 (⒈⒉⒊)");
    symbolcombobox->addItem("编号 ( a.b.c.)");
    symbolcombobox->addItem("编号 ( A.B.C.)");
    symbolcombobox->addItem("编号 (ⅰ.ⅱ.ⅲ.)");
    symbolcombobox->addItem("编号 (Ⅰ.Ⅱ.Ⅲ.)");
    symbolcombobox->setStatusTip("段落加标号或编号");
    connect(symbolcombobox,&QComboBox::activated,this,&Myword::textStyle);
    comtoolbar->addWidget(symbolcombobox);

    fontcombobox=new QFontComboBox(this);
    comtoolbar->addWidget(fontcombobox);
    fontcombobox->setStatusTip("更改字体");
    // 使用SIGNAL和SLOT宏

    connect(fontcombobox, &QFontComboBox::currentTextChanged,
            this, &Myword::textFamily);

    sizecombobox=new QComboBox(this);
    comtoolbar->addWidget(sizecombobox);
    sizecombobox->setEditable(true);//允许用户输入字体大小
    sizecombobox->setStatusTip("更改字体");

    QFontDatabase db;//获取字体数据库，访问系统字体信息

    foreach(int size,db.standardSizes())//qt循环宏，遍历标准大小列表
    {
        sizecombobox->addItem(QString::number(size));
    }
    // 使用SIGNAL和SLOT宏
    connect(sizecombobox, &QComboBox::currentTextChanged,
            this, &Myword::textSize);
    sizecombobox->setCurrentIndex(sizecombobox->findText(QString::number(QApplication::font().pointSize())));//设置当前文字的大小

}//创建工具条
void Myword::createStatusBars()
{
    //设置状态条的初始值
    statusBar()->showMessage("就绪状态");
}//创建状态条
void Myword::enabledText()
{   /* QTextCursor cursor=this->textCursor();
    if()*/
    boldAct->setEnabled(true);
    italicAct->setEnabled(true);
    underlineAct->setEnabled(true);
    leftAlignAct->setEnabled(true);
    centerAct->setEnabled(true);
    rightAlignAct->setEnabled(true);
    justifyAct->setEnabled(true);
    colorAct->setEnabled(true);

}//使得格式菜单项下各个子菜单可用，就是选中内容了才能使用action
void Myword::fontChanged(const QFont&f)
{
    fontcombobox->setCurrentIndex(fontcombobox->findText(QFontInfo(f).family()));//下拉框更新
    sizecombobox->setCurrentIndex(sizecombobox->findText(QString::number(f.pointSize())));

    boldAct->setChecked(f.bold());
    italicAct->setChecked(f.italic());
    underlineAct->setChecked(f.underline());
}//字体更改
void Myword::colorChanged(const QColor&c)
{  QPixmap color(16,16);
    color.fill(c);
    colorAct->setIcon(QIcon(color));

}//颜色更改
void Myword::alignmentChanged(Qt::Alignment a){}//对齐判断
void Myword::fileNew()
{
    MyChild*child=this->createMyChild();
    child->newFile();
    child->show();

}//新建
void Myword::fileOpen()
{
    MyChild*child=this->createMyChild();
    child->loadFile(QFileDialog::getOpenFileName());
    child->show();


}//打开文件
void Myword::fileSave()
{
    if(activeMychild()&&activeMychild()->save());//保存当前文档
    statusBar()->showMessage(tr("Word文档保存成功."),2000);

}//文件保存
void Myword::fileSaveAs()
{
    if(activeMychild()&&activeMychild()->saveAs());
    statusBar()->showMessage(tr("Word文档另存为成功."));
}//文件另存为
void Myword::filePrint()
{
    MyChild*child=activeMychild();
    if(!child)
    {
        QMessageBox::information(this,tr("提示"),tr("没有活动文档！"));
        return;
    }
    //创建打印机对象
    QPrinter printer(QPrinter::HighResolution);
    //创建打印对话框
    QPrintDialog printDialog(&printer,this);
    if(printDialog.exec()==QDialog::Accepted)
    {
        child->print(&printer);
        //更新状态栏显示打印信息
        statusBar()->showMessage(tr("文档已发送到打印机"),2000);
    }
    else{
        //用户取消打印
        statusBar()->showMessage(tr("打印取消"),2000);
    }

}//文件打印
void Myword::filePrintPreview()

{
    MyChild*child=activeMychild();
    if(!child)
    {
        QMessageBox::information(this,tr("提示"),tr("没有打开任何文档，无法预览！"));
        return;
    }
    //创建打印机对象
    QPrinter printer(QPrinter::HighResolution);//高分辨率模式
    //创建打印预览对话框
    QPrintPreviewDialog previewDialog(&printer,this);
    previewDialog.setWindowTitle(tr("打印预览"));
    previewDialog.setMaximumSize(800,600);
    //连接打印预览对话框的绘制请求到我们的槽函数
    connect(&previewDialog, SIGNAL(paintRequested(QPrinter*)),
            this, SLOT(printPreview(QPrinter*)));
    // 显示预览对话框
    previewDialog.exec();
}//文件打印预览
void Myword::printPreview(QPrinter *printer)
{
    MyChild *child = activeMychild();
    if (child) {
        child->print(printer);//进行绘制到打印预览中
    }
}//打印预览

//文件编辑菜单
void Myword::undo()
{
    if(activeMychild())
    {
        activeMychild()->undo();}
}//撤销
void Myword::redo()
{
    if(activeMychild())
    {
        activeMychild()->redo();}
}//重做
void Myword::cut()
{
    if(activeMychild())
    {
        activeMychild()->cut();//对当前编辑文档调用QTextEdit的cut（）
    }

}//剪切
void Myword::copy()
{
    if(activeMychild())
    {
        activeMychild()->copy();//对当前编辑文档调用QTextEdit的copy（）
    }
}//复制
void Myword::paste()
{
    if(activeMychild())
    {
        activeMychild()->paste();//对当前编辑文档调用QTextEdit的paste（）
    }
}//粘贴

//关于菜单
void Myword::about(){}//关于

//工具栏选项
void Myword::textBold()
{
    if(activeMychild())
    {  //创建文本格式对象
        QTextCharFormat fm;
        //根据按钮决定用什么格式
        if(boldAct->isChecked())
        {
            fm.setFontWeight(QFont::Bold);
        }
        else
        {
            fm.setFontWeight(QFont::Normal);
        }

        //应用到当前文本
        activeMychild()->mergeFormationOnWordOrSelection(fm);//对选中内容进行设置

    }

}//加粗
void Myword::textItalic()
{
    if(activeMychild())
    {  //创建文本格式对象
        QTextCharFormat fm;
        //根据按钮决定用什么格式
        if(italicAct->isChecked())
        {fm.setFontItalic(true);}
        else
        { fm.setFontItalic(false);}


        //应用到当前文本
        activeMychild()->mergeFormationOnWordOrSelection(fm);//对选中内容进行设置

    }
} // 斜体
void Myword::textUnderline()
{
    if(activeMychild())
    {  //创建文本格式对象
        QTextCharFormat fm;
        //根据按钮决定用什么格式

        if(underlineAct->isChecked())
        {
            fm.setFontUnderline(true);


        }
        else
        {
            fm.setFontUnderline(false);
        }

        //应用到当前文本
        activeMychild()->mergeFormationOnWordOrSelection(fm);//对选中内容进行设置

    }
}

void Myword::textAlign(QAction *a)
{
    if(activeMychild())
    {   if(a==leftAlignAct)
            activeMychild()->setAlign(1);
        else if(a==centerAct)
            activeMychild()->setAlign(2);
        else if(a==rightAlignAct)
            activeMychild()->setAlign(3);
        else if(a==justifyAct)
            activeMychild()->setAlign(4);
    }

} // 下划线
void Myword::textStyle(int styleIndex)//设置列表格式
{
    if(activeMychild())
    {
        activeMychild()->setStyle(styleIndex);
    }
} // 文本样式
void Myword::textFamily(const QString &f)
{
    qDebug() << "字体更改为:" << f;  // 添加调试信息

    if (activeMychild() && !f.isEmpty()) {
        QTextCharFormat fmt;
        fmt.setFontFamily(f);
        activeMychild()->mergeFormationOnWordOrSelection(fmt);

        // 更新状态栏显示
        statusBar()->showMessage(tr("字体已设置为: %1").arg(f), 2000);
    }
} // 函数设置文本格式
void Myword::textSize(const QString &p)
{   bool ok;
    qreal pointsize=p.toDouble(&ok);
    if(ok&&pointsize>0)
    {
        QTextCharFormat fmt;
        fmt.setFontPointSize(pointsize);
        if(activeMychild())
        {activeMychild()->mergeFormationOnWordOrSelection(fmt);}
    }
    else
    {
        qDebug()<<"无效字体"<<p;
    }
} // 文本大小
void Myword::textColor()
{
    if(activeMychild())
    {
        QColor color=QColorDialog::getColor(activeMychild()->textColor(),this);
        QTextCharFormat fmt;
        if(!color.isValid())
            return ;
        fmt.setForeground(color);
        activeMychild()->mergeFormationOnWordOrSelection(fmt);
        colorChanged(color);

    }
} // 文本颜色

void Myword::updateMenus()
{  //通过窗口判断是否右文档打开，并且根据这个判断来决定按钮是否启用
    bool hasMyChild=(activeMychild()!=0);
    saveAct->setEnabled(hasMyChild);
    saveAsAct->setEnabled(hasMyChild);
    printAct->setEnabled(hasMyChild);
    printPrevAct->setEnabled(hasMyChild);

    pasteAct->setEnabled(hasMyChild);

    closeAct->setEnabled(hasMyChild);
    closeAllAct->setEnabled(hasMyChild);
    tileAct->setEnabled(hasMyChild);
    cascadeAct->setEnabled(hasMyChild);
    nextAct->setEnabled(hasMyChild);
    previousAct->setEnabled(hasMyChild);
    separatorAct->setVisible(hasMyChild);

    //当有窗口打开是，判断是否有内容选中再决定是否开启按钮
    bool hasSelection=(activeMychild()&&activeMychild()->textCursor().hasSelection());


    cutAct->setEnabled(hasSelection);
    copyAct->setEnabled(hasSelection);

    boldAct->setEnabled(hasSelection);
    italicAct->setEnabled(hasSelection);
    underlineAct->setEnabled(hasSelection);
    leftAlignAct->setEnabled(hasSelection);
    centerAct->setEnabled(hasSelection);
    rightAlignAct->setEnabled(hasSelection);
    justifyAct->setEnabled(hasSelection);
    colorAct->setEnabled(hasSelection);


}//在使用一些选项后更新菜单

MyChild*Myword::createMyChild()
{
    MyChild*child=new MyChild(this);
    mdiArea->addSubWindow(child);

    return child;
}//创建子窗口对象


MyChild*Myword::activeMychild()
{
    //获取当前活动的MDI子窗口
    //就像是在多个word文档找出那个正在编辑的文档
    if(QMdiSubWindow*activeSubWindow=mdiArea->activeSubWindow())
    {
        return qobject_cast<MyChild*>(activeSubWindow->widget());//获取当前激活文档窗口对象，然后转换为文档对象

    }
    return 0;

}//激活子窗口
void Myword::setActiveSubWindow(QWidget*window)
{
    if(window)
    {
        mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(window));
    }


} // 设置active激活窗口
QMdiSubWindow *Myword::findMyChild(const QString &fileName)
{
    // foreach(mdiArea->subWindowList())
    return nullptr;
}// 查找子窗口
void Myword::updateWindowMenu()
{
    //主要为了更新窗口菜单的文档，获取当前所有在mdi的文档放入窗口菜单
    windowMenu->clear();
    windowMenu->addAction(closeAct);
    windowMenu->addAction(closeAllAct);
    windowMenu->addSeparator();

    windowMenu->addAction(tileAct);
    windowMenu->addAction(cascadeAct);
    windowMenu->addSeparator();

    windowMenu->addAction(nextAct);
    windowMenu->addAction(previousAct);
    windowMenu->addAction(separatorAct);
    //获取当前所有打开文档的标题，存入窗口菜单
    QList<QMdiSubWindow*>windows=mdiArea->subWindowList();
    separatorAct->setVisible(true);//设置分割线
    for(int i=0;i<windows.size();i++)
    {
        QMdiSubWindow* window = windows.at(i);
        MyChild*child=qobject_cast<MyChild*>(windows.at(i)->widget());
        QString text;

        text=tr("%1 %2").arg(i+1).arg(child->userFriendlyCurrentFile());//获取文件名
        QAction*action=new QAction(text);
        windowMenu->addAction(action);
        //更换文档准星
        action->setCheckable(true);
        action->setChecked(child==activeMychild());
        connect(action, &QAction::triggered, this, [this, window]() {
            mdiArea->setActiveSubWindow(window);//实现更换action来换窗口
        });
        //windowMapper->setMapping(action,windows.at(i));
        // 信号映射器(QSignalMapper)：解决多个信号连接到同一个槽时区分信号来源的问题
        //  connect(action, ..., windowMapper, ...)：当菜单项被点击时，触发映射器的map()信号
        //map信号触发更换活动文档
        //setMapping(action, windows.at(i))：建立菜单项与对应窗口的映射关系
        //最终效果：点击菜单项 → 映射器发出mapped(QWidget*)信号 → 激活对应窗口
    }


}//更新窗口菜单
